package com.share.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * author:caifan
 * date:2020/2/4
 */
public class BellEventSource {
    private List<BellEventListener> listenerList;//监听器

    public BellEventSource() {
        listenerList = new ArrayList<>();
    }

    /**
     * 添加监听器
     * @param ren
     */
    public void addPersonListener(BellEventListener ren) {
        listenerList.add(ren);
    }

    public void ring(boolean sound) {
        String type = sound ? "上课铃" : "下课铃";
        System.out.println(type + "响！");
        RingEvent event = new RingEvent(this, sound);
        notifies(event);
    }

    public void notifies(RingEvent event) {
        for (BellEventListener listener : listenerList) {
            listener.heardBell(event);
        }
    }
}
